﻿using GN.Pay.TradeChannel.DefaultImpl;
using GN.Pay.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.TradeChannel.UnionPay.Response
{
    [Serializable]
    public class RefundResponse : AResponse
    {
        private readonly ITradeChannel channel;
        private readonly UnionPayConfigure config;
        private readonly IRefundApplyInfo apply;
        private IDictionary<string, string> htmlDic;
        /// <summary>
        /// 获取响应字符
        /// </summary>
        private readonly string responseString;

        public RefundResponse(ITradeChannel channel,
           UnionPayConfigure config,
           IRefundApplyInfo apply,
           string reponsestr)
        {
            this.apply = apply;
            this.channel = channel;
            this.config = config;
            if (string.IsNullOrWhiteSpace(reponsestr))
            {
                this.channel.ThrowInterfaceException(Format_ErrorMessage);
            }
            this.responseString = reponsestr.Trim();
            this.channel = channel;
            this.config = config;
            try
            {
                htmlDic = UrlDic(reponsestr);
            }
            catch
            {
                throw channel.ThrowInterfaceException(Format_ErrorMessage);
            }
            this.Load();
        }

        private static string Format_ErrorMessage = "响应结果格式不正确。";

        /// <summary>
        /// 签名验证
        /// </summary>
        /// <returns></returns>
        public bool SignVeryfy()
        {
            string sign;
            if (!this.htmlDic.TryGetValue(sign_Key, out sign) || string.IsNullOrWhiteSpace(sign))
            {
                return false;
            }
            this.htmlDic.Remove(sign_Key);
            if (this.htmlDic.Count == 0)
            {
                return false;
            }
            SignDictionary sortDic = new SignDictionary(this.htmlDic);
            string content = sortDic.UrlLinkString();
            return SecurityUtils.SignVerify(config.ConfigureInfo, sign, content);
        }

        private void Load()
        {
            this.LoadProperty(htmlDic);
            if (string.IsNullOrWhiteSpace(RespCode))
            {
                throw this.channel.ThrowInterfaceException(Format_ErrorMessage);
            }
        }

        /// <summary>
        /// 获取是否成功
        /// </summary>
        public bool IsSuccess()
        {
            return "00".Equals(RespCode);
        }

        /// <summary>
        /// 获取响应代码
        /// </summary>
        public string RespCode { get; private set; }

        /// <summary>
        /// 获取响应消息
        /// </summary>
        public string RespMsg { get; set; }

        /// <summary>
        /// 查询id(退款交易号)
        /// </summary>
        public string QueryId { get; set; }

        /// <summary>
        /// 交易金额
        /// </summary>
        public long TxnAmt { get; set; }

        /// <summary>
        /// 转为结果
        /// </summary>      
        /// <returns></returns>
        public ITradeRefundApplyResult ToResult()
        {
            var state = TradeApplyResultState.Apply_Success_Wiat_Notify;
            var msg = "";
            if (!this.IsSuccess())
            {
                if (RespMsg == null)
                {
                    RespMsg = "";
                }
                state = TradeApplyResultState.Apply_Error;
                msg = "(" + this.RespCode + ")" + this.RespMsg;
            }
            return new TradeRefundApplyResult(
                this.channel,
                apply.RefundNo,
                apply.TradeNo,
                (decimal)TxnAmt * 100,
                0m, null,
                state,
                msg);
        }
    }
}
